//
//  HZNetworking.swift
//  555wobo
//
//  Created by 黄某某 on 15/12/17.
//  Copyright © 2015年 黄某某. All rights reserved.
//

import UIKit

import AFNetworking

class HZNetworking: AFHTTPSessionManager {
//    static let sharedInsatance: HZNetworking = {
//        let baseURL = NSURL(fileURLWithPath: "https://api.weibo.com/")
//        let tools = HZNetworking(baseURL: baseURL)
//        
//        return tools
//    }()
//    72824953
//    ca62ba9f210de7bfbd02fd1088faec84
    static let sharedInstance: HZNetworking = HZNetworking()
    
    // 不让别人访问构造方法
    private init () {
        // 没有初始化的属性
        let baseURL = NSURL(string: "https://api.weibo.com/")!
        // 在构造函数里面子类不能调用父类的便利构造函数,需要调用父类的指定构造函数
        //        super.init(baseURL: baseURL)
        super.init(baseURL: baseURL, sessionConfiguration: nil)
        // 方法的调用必须在 super.init 只有 调用父类的super.init之后,对象才创建完毕,对象创建完毕才能调用对象的方法
        
        // 设置响应的序列化器,支持text/plain
//        self.responseSerializer.acceptableContentTypes?.insert("text/plain")
        self.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as? Set<String>
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

   
    private let client_id = "72824953"
    private let client_secret = "ca62ba9f210de7bfbd02fd1088faec84"
    let redirect_uri = "http://www.baidu.com"
    func oAuthURL() -> NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        return NSURL(string: urlString)!
    }
    func loadAccessToken(code: String,finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {

        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
        
        POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            let dict = result as? [String : AnyObject]
            finished(result: dict, error: nil)
            }) { (_, error) -> Void in
            finished(result: nil, error: error)
        }
    }
    func loadUserInfo(finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        guard let access_token = HZUserAccount.loadUserAccount()?.access_token else{
            print("access_token没值")
            return
        }
        
        guard let uid = HZUserAccount.loadUserAccount()?.uid else{
            print("uid没值")
            return
        }

        let urlString = "2/users/show.json"
        
        let parameters = [
            "access_token": access_token,
            "uid": uid
        ]
        
        
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            let dict = result as? [String: AnyObject]
            finished(result: dict, error: nil)
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
        }
    }
    func loadStatus(finished: (result: [String: AnyObject]?, error: NSError?) -> ()){
        guard let access_token = HZUserAccount.loadUserAccount()?.access_token else {
            return
        }
        let urlString = "2/statuses/home_timeline.json"
        let parameters = [
        "access_token":access_token
        ]
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            let dict = result as? [String :AnyObject]
            finished(result: dict, error: nil)
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
        }
    }
}



